import 'package:flutter/material.dart';
import 'package:guoshuyu_flutter_project/paper/color_selector.dart';
import 'package:guoshuyu_flutter_project/paper/conform_dialog.dart';
import 'package:guoshuyu_flutter_project/paper/model.dart';
import 'package:guoshuyu_flutter_project/paper/paper_app_bar.dart';
import 'package:guoshuyu_flutter_project/paper/paper_painter.dart';
import 'package:guoshuyu_flutter_project/paper/stork_width_selector.dart';

class Paper extends StatefulWidget {
  const Paper({super.key});

  @override
  State<StatefulWidget> createState() => _PaperState();
}

class _PaperState extends State<Paper> {
  final List<Line> _lines = []; //线列表
  final List<Line> _historyLines = []; //历史线列表

  int _activeColorIndex = 0; //颜色激活索引
  int _activeStorkWidthIndex = 0; //线宽激活索引
  final List<Color> supportColors = [
    Colors.black,
    Colors.red,
    Colors.orange,
    Colors.yellow,
    Colors.green,
    Colors.blue,
    Colors.purple
  ];
  //支持的线条粗细
  final List<double> supportStorkWidths = [1, 2, 4, 6, 8, 10];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: PaperAppBar(
          onClear: _clear,
          onBack: _lines.isEmpty ? null : _back,
          onRevocation: _historyLines.isEmpty ? null : _revocation,
        ),
        body: Stack(
          children: [
            GestureDetector(
              onPanStart: _onPanStart,
              onPanUpdate: _onPanUpdate,
              child: CustomPaint(
                  painter: PaperPainter(lines: _lines),
                  child: ConstrainedBox(
                    constraints: const BoxConstraints.expand(),
                  )),
            ),
            Positioned(
              bottom: 0,
              width: MediaQuery.of(context).size.width,
              child: Row(
                children: [
                  Expanded(
                    child: ColorSelector(
                      supportColors: supportColors,
                      activeIndex: _activeColorIndex,
                      onSelect: _onSelectColor,
                    ),
                  ),
                  StorkWidthSelector(
                    supportStorkWidths: supportStorkWidths,
                    color: supportColors[_activeColorIndex],
                    activeIndex: _activeStorkWidthIndex,
                    onSelect: _onSelectStorkWidth,
                  ),
                ],
              ),
            ),
          ],
        ));
  }

  void _clear() {
    // _showClearDialog();
    _lines.clear();
  }

  void _showClearDialog() {
    String msg = "您的当前操作会清空绘制内容，是否确定删除!";
    showDialog(
        context: context,
        builder: (ctx) => ConformDialog(
              title: '清空提示',
              conformText: '确定',
              msg: msg,
              onConform: _clear,
            ));
  }

  void _onPanStart(DragStartDetails details) {
    _lines.add(Line(
        points: [details.localPosition],
        color: supportColors[_activeColorIndex],
        strokeWidth: supportStorkWidths[_activeStorkWidthIndex]));
  }

  void _onPanUpdate(DragUpdateDetails details) {
    Offset point = details.localPosition;
    double distance = (_lines.last.points.last - point).distance;
    if (distance > 5) {
      print("====details:${details.localPosition}========");
      _lines.last.points.add(details.localPosition);
      setState(() {});
    }
  }

  void _onSelectStorkWidth(int index) {
    if (index != _activeStorkWidthIndex) {
      setState(() {
        _activeStorkWidthIndex = index;
      });
    }
  }

  void _onSelectColor(int index) {
    if (index != _activeColorIndex) {
      setState(() {
        _activeColorIndex = index;
      });
    }
  }

  void _back() {
    Line line = _lines.removeLast();
    _historyLines.add(line);
    setState(() {});
  }

  void _revocation() {
    Line line = _historyLines.removeLast();
    _lines.add(line);
    setState(() {});
  }
}
